import Vue from 'vue'
Vue.prototype.$WX = {
	getUserProfile(options){
		return new Promise((resolve,reject) => {
			uni.getUserProfile({
				...options,
				success: (res) => {
					resolve(res)
				},
				fail:(res) => {
					reject(res)
				}
			})
		})
		
	},
	getCode(){
		return new Promise((resolve, reject) => {
			uni.login({
				success(data) {
					resolve(data.code)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	
	
	login: () => { //微信小程序的登录（获取code）
		return new Promise((resolve, reject) => {
			uni.login({
				success(data) {
					resolve(data.code)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	location: ({
		type = 'gcj02',
		isHighAccuracy = false
	}) => {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type,
				isHighAccuracy,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	map: (latitude, longitude, type) => {
		return new Promise((resolve, reject) => {
			uni.chooseLocation({
				type,
				latitude,
				longitude,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	setting: wx.getSetting,
	authorize(name) { //授权
		return new Promise((resolve, reject) => {
			wx.getSetting().then(res => {
				if (res.authSetting && (res.authSetting[name])) { //已授权
					resolve()
				} else if (res.authSetting && (res.authSetting[name]) == null) { //未授权
					wx.authorize({
						scope: name,
						success() { //已授权
							resolve()
						},
						fail(err) { //拒绝授权
							wx.showToast({
								title: '拒绝授权',
								icon: 'none',
								duration: 1000
							})
						}
					})
				} else { //拒绝授权
					reject()
				}
			})
		})
	},
	requestSubscribeMessage(tmplIds) {
		return new Promise((resolve, reject) => {
			wx.requestSubscribeMessage({
				tmplIds,
				success: (res) => {
					resolve(res)
				}
			})
		})
	}
}
Vue.prototype.scanCode = (option) => {
	console.log(option)
	return new Promise((resolve, reject) => {
		uni.scanCode({
			...option,
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				reject(err)
			}
		});
	})
}
